﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Tunnel_Cross_Section.option_frm;
using Microsoft.Win32;

namespace Tunnel_Cross_Section.相关类
{
    public class DataBaseConfig
    {
        [DllImport("kernel32.dll")]
        public static extern int GetPrivateProfileString(string section, string key,
              string def, StringBuilder retval, int size, string filepath);

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);


        private string path = System.Windows.Forms.Application.StartupPath + @"\config.ini";
        public DataBaseConfig()
        {
            if (Convert.ToBoolean(Getvalue("new")) == true)
            {
                SetValue("new", "false");
                SetDataBaseType();
            }
        }

        public void SetDataBaseType()
        {
            message_frm frm = new message_frm("选择数据库?\r\n'是'选择远程\r\n'否'选择本地", "选择数据库", 1);
            frm.ShowDialog();
            if (frm.yesorno == true)
            {
                SetValue("type", "remote");
            }
            else
            {
                SetValue("type", "local");
            }
            frm.Close();
        }
        public DataBase ConnectDatabase()
        {
            string str = Getvalue("type");
            if (str.ToString() == DataType.local.ToString())
            {
                return GetLocal();
            }
            else
            {
                return GetRemote();
            }

        }
        private void SetValue(string cato, string value)
        {
            WritePrivateProfileString("config", cato, value, path);
        }
        private string Getvalue(string cato)
        {
            StringBuilder str = new StringBuilder(40);
            string def = "";

            GetPrivateProfileString("config", cato, def, str,
                40, path);
            return str.ToString();
        }
        private DataBase GetRemote()
        {
            DataBase db = null;
            string database, user, psw, port, ip;
            ip = Getvalue("ip");
            port =  Getvalue("port");
            database = Getvalue("database");
            user = Getvalue("user");
            psw = Getvalue("psw");
            if (DataBase.ConnectToDataBase("sqlserver", ip,
                port, "",
                    "\\", user,
                     psw,
                     database, false, ref db))
            {
                return db;
            }
            else
            {
                return null;
            }
        }

        private DataBase GetLocal()
        {
            DataBase db = null;
            string computer = System.Environment.MachineName;
            string server = Getvalue("servername");
            if (server != null && server.Length > 0) computer = server;
            RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server");
            String[] instances = (String[])rk.GetValue("InstalledInstances");
            if (instances.Length > 0)
            {
                if (instances[0] != "MSSQLSERVER")
                {
                    computer = computer + "\\" + instances[0];
                }
            }
            else
            {
                message_frm frm = new message_frm("本机似乎未安装数据库!", "错误", 1);
                frm.ShowDialog();
                return null;
            }
            if (DataBase.ConnectToDataBase("sqlserver", "",
                     computer, "", "", "cross_section", true, ref db))
            {
                return db;
            }
            else
            {
                return null;
            }
        }
    }

    enum DataType
    {
        local,
        remote,
    }
}
